import { createStore, applyMiddleware, compose, combineReducers } from 'redux';
import thunk from 'redux-thunk';
import createLogger from 'redux-logger';
import rootReducer from '../reducers/index';
import api from '../middleware/api';
import DevTools from '../containers/DevTools';
import { routerReducer, routerMiddleware } from 'react-router-redux'
import { browserHistory } from 'react-router';


const configureStore = preloadedState => {
  const middleware = routerMiddleware(browserHistory)
  const store = createStore(
    rootReducer,
    preloadedState,
    compose(
      applyMiddleware(thunk, api, createLogger(), middleware),
      DevTools.instrument()
    )
  )

  if (module.hot) {
    // Enable Webpack hot module replacement for reducers
    module.hot.accept('../reducers', () => {
      const nextRootReducer = require('../reducers').default;
      store.replaceReducer(nextRootReducer);
  });
  }

  return store;
}

export default configureStore;
